package cn.jdx;

import cn.jdx.servlet.MyServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@ServletComponentScan //(2).在启动器增加扫描
public class AppApplication {

    public static void main(String[] args) {
        SpringApplication.run(AppApplication.class, args);
    }
    //(3).注册Servlet的Bean
    @Bean
    public ServletRegistrationBean<MyServlet> getServletRegistrationBean(){
        ServletRegistrationBean<MyServlet> bean =
                new ServletRegistrationBean<>(new MyServlet());
        bean.setLoadOnStartup(1); //(4).设置启动优先级
        return bean;
    }
    /*
    [补充]：
    (1).在注册bean的时候，创建 ServletRegistrationBean 类时，还可以传入第二个参数：

    new ServletRegistrationBean<>(new MyServlet(),"/servlet");

    (2).如果这里配置了 urlMapping 那么在创建自己的 MyServlet 的时候就不需要再配置了：

    @WebServlet(name = "my_servlet",urlPatterns = "/servlet")

     */
}
